package com.design3.graduatethesissys.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.design3.graduatethesissys.common.utils.GetUserName;



@Controller
public class Login {

//	@GetMapping(value = "/Login")
//	public String demo(ModelMap data){
//	    data.addAttribute("info",1);
//	    return "Login";
//	}
	
//	@PostMapping(value = "/Login")
//	public String login(@RequestParam("userName")String username, @RequestParam("pwd")String password){
//		System.out.println(1);
//	    return "Index";
//	}
	
	
	@RequestMapping(value = "/Login")
	public String login() {
		// 已经登录的用户再次访问登录页面直接跳转Index
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if(auth instanceof AnonymousAuthenticationToken){
			return "Login";
		}else{
			return "design";
		}
	}
	
	@RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/Login";
    }
	
	@RequestMapping(value = "/design")
	public String design() {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if("[PERMISSION_ADMIN]".equals(auth.getAuthorities().toString())) {
			return "Admin/Index";
		} else if("[PERMISSION_TEACHER]".equals(auth.getAuthorities().toString())){
			return "Teacher/Index";
		} else if("[PERMISSION_STUDENT]".equals(auth.getAuthorities().toString())) {
			return "Student/Index";
		} else {
			return "Login";
		}
			
	}
	@RequestMapping(value = "/design/**")
	public String login1() {
		return "redirect:/design";
	}
}

	